﻿using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Windows.Media;
using CommonEntity.CommonImageEntities;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using ScienceAppService.Entities;
using ScienceAppService.ModuleInterface;

namespace ScienceAIApp.ViewModels.ToolBarPanel;
public class MarkListViewModel : ScienceBaseViewModel
{
    private IMarkDrawService _markDrawService;

    private IImageShowService _imageShowService;

    private int _currentPage;

    [Reactive] public int SelectedMarkTypeIndex { get; set; }

    public SegToolsEnum CurrenToolsEnum { get; set; }

    [Reactive] public SolidColorBrush SelectedBrush { get; set; }

    [Reactive] public int MarkSum { get; set; }
    [Reactive] public int Pin1 { get; set; }
    [Reactive] public int Pin2 { get; set; }
    [Reactive] public int Pin3 { get; set; }
    [Reactive] public int Pin4 { get; set; }
    [Reactive] public int Pin5 { get; set; }

    [Reactive] public bool IsSelectedPin1 { get; set; }
    [Reactive] public bool IsSelectedPin2 { get; set; }
    [Reactive] public bool IsSelectedPin3 { get; set; }
    [Reactive] public bool IsSelectedPin4 { get; set; }
    [Reactive] public bool IsSelectedPin5 { get; set; }

    //[Reactive] public bool IsPreVisible { get; set; }
    //[Reactive] public bool IsNextVisible { get; set; }

    /// <summary>
    /// 选中标记点
    /// </summary>
    public ReactiveCommand<int, Unit> SelectMarkPointCommand { get; set; }

    /// <summary>
    /// 前后翻页
    /// </summary>
    public ReactiveCommand<bool, Unit> MarkListSwitchCommand { get; set; }


    public ReactiveCommand<Unit, Unit> DeleteMarkPointCommand { get; set; }

    public ReactiveCommand<Unit, Unit> AiSegCommand { get; set; }



    public MarkListViewModel(IMarkDrawService markDrawService, IImageShowService imageShowService)
    {

        SelectedMarkTypeIndex = 0;

        _currentPage = 0;

        _markDrawService = markDrawService;

        _imageShowService = imageShowService;

        // 标注类型
        this.WhenAnyValue(t => t.SelectedMarkTypeIndex).Do(si =>
        {
            switch (si)
            {
                case 0:
                    CurrenToolsEnum = SegToolsEnum.MarkNone;
                    _markDrawService.SetMarkOrDrawType(SegToolsEnum.MarkNone);
                    SelectedBrush = Brushes.LightSeaGreen;
                    break;
                case 1:
                    CurrenToolsEnum = SegToolsEnum.MarkULNA;
                    SelectedBrush = Brushes.IndianRed;
                    _markDrawService.SetMarkOrDrawType(SegToolsEnum.MarkULNA);
                    break;
                case 2:
                    CurrenToolsEnum = SegToolsEnum.MarkRADIUS;
                    SelectedBrush = Brushes.Yellow;
                    _markDrawService.SetMarkOrDrawType(SegToolsEnum.MarkRADIUS);
                    break;
                case 3:
                    CurrenToolsEnum = SegToolsEnum.MarkTFCC;
                    SelectedBrush = Brushes.ForestGreen;
                    _markDrawService.SetMarkOrDrawType(SegToolsEnum.MarkTFCC);
                    break;
            }

            _currentPage = 0;
            FrushMarkList(_currentPage);

            if (AppServiceEnv.ImageInfo != null && AppServiceEnv.ImageInfo.Count > 0)
                _imageShowService.FrushAllOrientationMats(new Point3d()
                {
                    X = (int)(AppServiceEnv.ImageInfo.Width / 2),
                    Y = (int)(AppServiceEnv.ImageInfo.Height / 2),
                    Z = (int)(AppServiceEnv.ImageInfo.Count / 2)
                });
        }).Subscribe();

        //选中标记点的操作
        SelectMarkPointCommand = ReactiveCommand.CreateFromTask<int>( async markIndex =>
        {
            IsSelectedPin1 = false;
            IsSelectedPin2 = false;
            IsSelectedPin3 = false;
            IsSelectedPin4 = false;
            IsSelectedPin5 = false;

            var num = 0;
            switch (markIndex)
            {
                case 1:
                    IsSelectedPin1 = true;
                    num = Pin1;
                    break;
                case 2:
                    IsSelectedPin2 = true;
                    num = Pin2;
                    break;
                case 3:
                    IsSelectedPin3 = true;
                    num = Pin3;
                    break;
                case 4:
                    IsSelectedPin4 = true;
                    num = Pin4;
                    break;
                case 5:
                    IsSelectedPin5 = true;
                    num = Pin5;
                    break;
            }

            var selectP = _markDrawService.ChooseMarkPoint(num);
            await _imageShowService.FrushAllOrientationMats(selectP);
        });

        // flag == true 表示 pre page
        MarkListSwitchCommand = ReactiveCommand.Create<bool>(flag =>
        {
            if (flag)
            {
                FrushMarkList(_currentPage - 1);
            }
            else
            {
                FrushMarkList(_currentPage + 1);
            }
        });

        DeleteMarkPointCommand = ReactiveCommand.CreateFromTask(async  () =>
        {
            var delP = _markDrawService.DeleteMarkPoint();
            FrushMarkList(_currentPage);
            await _imageShowService.FrushAllOrientationMats(delP!);
        });

        _markDrawService.FrushMarksSubMenuEvent += FrushMarksHandler;

        AiSegCommand = ReactiveCommand.CreateFromTask(async () => 
        {
            await _markDrawService.AiSeg();
        });

    }

    void FrushMarksHandler(IList<Point3d> points)
    {
        MarkSum = points.Count;
        var count = points.Count / 5;
        var radom = points.Count % 5;
        if (radom == 0 && count > 0) count--;
        FrushMarkList(count);
    }

    void FrushMarkList(int num)
    {
        if (num < 0) return;
        var rs = _markDrawService.GetMarkPoints(num);
        if(rs == null) return;

        RxApp.MainThreadScheduler.Schedule(() =>
        {
            if (rs.Count <= 0)
            {
                Pin1 = -1;
                Pin2 = -1;
                Pin3 = -1;
                Pin4 = -1;
                Pin5 = -1;

                IsSelectedPin1 = false;
                IsSelectedPin2 = false;
                IsSelectedPin3 = false;
                IsSelectedPin4 = false;
                IsSelectedPin5 = false;
                return;
            }
            Pin1 = rs[0].Number;
            Pin2 = rs[1].Number;
            Pin3 = rs[2].Number;
            Pin4 = rs[3].Number;
            Pin5 = rs[4].Number;

            IsSelectedPin1 = rs[0].IsSelected;
            IsSelectedPin2 = rs[1].IsSelected;
            IsSelectedPin3 = rs[2].IsSelected;
            IsSelectedPin4 = rs[3].IsSelected;
            IsSelectedPin5 = rs[4].IsSelected;

            if (rs.Any(r => r.Number != -1))
            {
                _currentPage = num;
            }
        });
    }

}
